//
//  LXMallShareView.m
//  绿稀全球购
//
//  Created by 王敏 on 16/6/11.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXMallShareView.h"

#import "LXOptionButton.h"

@interface LXMallShareView()

@property(nonatomic,strong)UIButton * cancelButton;

@property(nonatomic,strong)NSArray<LXOptionButton *>* shareButtons;

@property(nonatomic,strong)UIView * coverView;

@end

@implementation LXMallShareView

-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = RGBColor(195, 194, 194);
        
        UIButton * cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.height-50, self.width, 50)];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        cancelButton.backgroundColor = [UIColor whiteColor];
        [cancelButton addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        cancelButton.tag = 5;
        [self addSubview:cancelButton];
        self.cancelButton = cancelButton;
        
        
        NSArray * titleArray = @[@"微信好友",@"朋友圈",@"手机QQ",@"QQ空间",@"微博"];
        NSArray * imageArray = @[@"share_weixin",@"share_friend",@"share_QQ",@"share_zone",@"share_weibo"];
        NSMutableArray * marr = [NSMutableArray array];
        for (int i = 0 ; i<5; i++) {
            LXOptionButton * shareButton = [[LXOptionButton alloc] init];
            [shareButton setTitle:titleArray[i] forState:UIControlStateNormal];
            [shareButton setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
            [shareButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            shareButton.titleLabel.font = [UIFont systemFontOfSize:10];
            shareButton.tag = i;
            [shareButton addTarget:self action:@selector(shareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:shareButton];
            [marr addObject:shareButton];
            
            
            self.duration = 0.2;
        }
        self.shareButtons = marr;
        
    }
    return self;
}


-(void)shareButtonClick:(LXOptionButton*)button{
    if(self.handle){
        self.handle(button);
    }
    
    
    if(self.shareMethon){
        self.shareMethon(button.tag);
    }
}


-(void)cancelButtonClick:(UIButton *)button{
    [self hidden];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat space = self.width * 0.01;
    CGFloat buttonWidth = (self.width - (self.shareButtons.count+1)*space)/self.shareButtons.count;
    CGFloat buttonHeight = buttonWidth*1.428;
    CGFloat buttonCenterY = (self.height - self.cancelButton.height)/2.0;
    
    for (int i = 0; i<self.shareButtons.count; i++) {
        LXOptionButton * shareButton  = self.shareButtons[i];
        shareButton.width = buttonWidth;
        shareButton.height = buttonHeight;
        shareButton.x = i*(space + buttonWidth)+space;
        shareButton.centerY = buttonCenterY;
        shareButton.style = LXOptionButtonStyleD_U;
    }
    
}


-(void)show{
    [[[UIApplication sharedApplication].delegate window] addSubview:_coverView];
    _coverView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.0];
    [self.coverView addSubview:self];
    self.y = self.coverView.height;
    [UIView animateWithDuration:self.duration animations:^{
        _coverView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
        self.y = self.coverView.height - self.height;
    }];
}

-(void)hidden{
    [UIView animateWithDuration:self.duration animations:^{
        _coverView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.0];
        self.y = self.coverView.height;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.coverView removeFromSuperview];
    }];
}

-(UIView*)coverView{
    if(_coverView == nil){
        _coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _coverView.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    }
    return _coverView;
}




















////需要自定义面板样式的开发者需要自己绘制UI，在对应的分享按钮中调用此接口
//[UMSocialData defaultData].extConfig.title = @"分享的title";
//[UMSocialData defaultData].extConfig.qqData.url = @"http://baidu.com";
//UMSocialUrlResource *urlResource = [[UMSocialUrlResource alloc] initWithSnsResourceType:UMSocialUrlResourceTypeImage url:
//                                    @"http://www.baidu.com/img/bdlogo.gif"];
//[[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQQ] content:@"分享内嵌文字" image:nil location:nil urlResource:urlResource presentedController:self completion:^(UMSocialResponseEntity *shareResponse){
//    if (shareResponse.responseCode == UMSResponseCodeSuccess) {
//        NSLog(@"分享成功！");
//    }
//}];
@end
